#include "colordesigner.h"
#include "ui_colordesigner.h"

extern QMap<QString,QColor> accessColorList;
extern QMap<QString,QColor> sampleColorList;
extern int ChartColorType;
ColorDesigner::ColorDesigner(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ColorDesigner)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::WindowCloseButtonHint);
    this->setAttribute(Qt::WA_QuitOnClose, false);
    this->setWindowModality(Qt::ApplicationModal);
#ifdef Q_OS_ANDROID
    this->setParent(parent);
    this->setWindowFlags(Qt:: Dialog | Qt:: FramelessWindowHint);
    this->setStyleSheet("QDialog{background-color: white; padding:20px; border: none; border: 10px solid rgb(200,219,243);}");
#else
    this->setStyleSheet("QDialog{background-color: white;}");
#endif
}

ColorDesigner::~ColorDesigner()
{
    delete ui;
}
void ColorDesigner::showEvent(QShowEvent *event){
#ifdef Q_OS_ANDROID
    if(this->parent() != nullptr)
        this->setGeometry(((QWidget*)parent())->width()/2-200, ((QWidget*)parent())->height()/2-this->height()/2, 400, 500);
#endif
    RefPage();
}

void ColorDesigner::on_pushButton_clicked()
{
    QString info= ui->comboBox_2->currentText();
    if(ui->comboBox->currentText()=="通道"){
        QColor oldColor= accessColorList.value(info);
        QColor color=QColorDialog::getColor(oldColor,this,"选择颜色");
        if(color!=oldColor){
            accessColorList.remove(info);
            accessColorList.insert(info,color);
            SetButtonColor(color);
        }
    }
    else if(ui->comboBox->currentText()=="样本名称"){
        QColor oldColor= sampleColorList.value(info);
        QColor color=QColorDialog::getColor(oldColor,this,"选择颜色");
        if(color!=oldColor){
            sampleColorList.remove(info);
            sampleColorList.insert(info,color);
            SetButtonColor(color);
        }
    }
}

void ColorDesigner::RefPage(){
    if(ChartColorType==0)//通道
    {
        ui->comboBox->setCurrentText("通道");

        ui->comboBox_2->blockSignals(true);
        QList<QString> info= accessColorList.keys();
        ui->comboBox_2->clear();
        ui->comboBox_2->addItems(info);
        ui->comboBox_2->blockSignals(false);
        ui->comboBox_2->setCurrentIndex(0);

        QString currentInfo= ui->comboBox_2->currentText();
        QColor color= accessColorList.value(currentInfo);
        SetButtonColor(color);

    }
    else if(ChartColorType==1)//样本名称
    {
        ui->comboBox->setCurrentText("样本名称");

        ui->comboBox_2->blockSignals(true);
        QList<QString> info= sampleColorList.keys();
        ui->comboBox_2->clear();
        ui->comboBox_2->addItems(info);
        ui->comboBox_2->blockSignals(false);

        QString currentInfo= ui->comboBox_2->currentText();
        QColor color= sampleColorList.value(currentInfo);
        SetButtonColor(color);
    }
}

void ColorDesigner::on_pushButton_3_clicked()
{
    this->close();
}

void ColorDesigner::on_comboBox_currentTextChanged(const QString &arg1)
{
    ui->comboBox_2->blockSignals(true);
    if(arg1=="通道")
    {
        ChartColorType=0;
        ui->comboBox_2->clear();
        QList<QString> info= accessColorList.keys();
        ui->comboBox_2->addItems(info);

        QString currentInfo= ui->comboBox_2->currentText();
        QColor color= accessColorList.value(currentInfo);
        SetButtonColor(color);
    }
    else if(arg1=="样本名称")
    {
        ChartColorType=1;
        ui->comboBox_2->clear();
        QList<QString> info= sampleColorList.keys();
        ui->comboBox_2->addItems(info);
        QString currentInfo= ui->comboBox_2->currentText();
        QColor color= sampleColorList.value(currentInfo);
        SetButtonColor(color);
    }
    ui->comboBox_2->blockSignals(false);
}

void ColorDesigner::on_comboBox_2_currentTextChanged(const QString &arg1)
{
    QColor color;
    if(ui->comboBox->currentText()=="通道")
    {
        color= accessColorList.value(arg1);

    }
    else if(ui->comboBox->currentText()=="样本名称")
    {
        color= sampleColorList.value(arg1);
    }
    SetButtonColor(color);
}

void ColorDesigner::on_pushButton_2_clicked()
{
    this->close();
}

void ColorDesigner::SetButtonColor(QColor color){
    QPalette palette1=ui->pushButton->palette();
    palette1.setColor(QPalette::Button,color);
    ui->pushButton->setPalette(palette1);
    ui->pushButton->setAutoFillBackground(true);
}
